package com.kuang.util;

/**
 * @ClassName StringUtil
 * @Description: TODO
 * @Author: Sunny
 * @Create: 2023-10-11 16:14
 * @Version 1.0
 **/
public class StringUtil {
    public static String removeTrailingZeros(String number) {
        if (number == null || number.isEmpty()) {
            return number;
        }

        int decimalIndex = number.indexOf('.');
        if (decimalIndex < 0) {
            number = number +".00";
            return number;
        }

        int lastNonZeroIndex = decimalIndex + 1;
        for (int i = decimalIndex + 1; i < number.length(); i++) {
            if (number.charAt(i) != '0') {
                lastNonZeroIndex = i + 1;
            }
        }
        String substring = number.substring(0, lastNonZeroIndex);
        int i = countCharactersAfter(substring, '.');
        if (i == 1) {
            substring = substring + "0";
        }
        return substring;
    }


    public static int countCharactersAfter(String input, char character) {
        if (input == null || input.isEmpty()) {
            return 0;
        }
        int count = 0;
        boolean found = false;
        for (int i = 0; i < input.length(); i++) {
            if (found) {
                count++;
            }

            if (input.charAt(i) == character) {
                found = true;
            }
        }

        return count;
    }
}